package controllers

import (
		 "golang/models"
        "github.com/astaxie/beego"
         "math/rand"
         // "encoding/json"
         "github.com/astaxie/beego/orm"
         "time"
         _"fmt"
         _"golang/bash"
        "github.com/astaxie/beego/logs"
        "github.com/astaxie/beego/session"

         
)

type MainController struct {
        beego.Controller
}

func (c *MainController) Get() {
        c.Data["Website"] = "beego.me"
        c.Data["Email"] = "astaxie@gmail.com"
        c.Data["go"] = "astaxie@gmail.com"
        c.TplName = "index.tpl"
}

type LoginController struct {
        beego.Controller
}
type Respose struct {
        Code int64
        Msg string
        Pwd string
        UserId int

}
type LoginRespose struct {
        Code int64
        Msg string
        Pwd string


}
type User struct {
        UserName string
        Pwd string
}
type List struct {
    UserId    int
    
}

func (c *LoginController) Post() {
        // var username string
        // var pwd string
        // var userId int
        // var  user User
        // data := c.Ctx.Input.RequestBody
         // json.Unmarshal(data, &user)
        // username := user.UserName
        // pwd :=  user.Pwd
        username:=c.GetString("username")
	    pwd:=c.GetString("pwd")
        if username == "" {
         c.Data["json"] = Respose{Code: 100,Msg: "登录失败，没有输入用户名",Pwd:pwd,UserId:0}
         c.ServeJSON()
         return
    }
    if pwd == "" {
         c.Data["json"] = Respose{Code: 100,Msg: "登录失败，没有输入密码",Pwd:pwd,UserId:0}
         c.ServeJSON()
         return
    }

    	o:=orm.NewOrm()
		  var list List

		err := o.Raw("SELECT user_id,username FROM users where username= ? and pwd = ? ",username,pwd).QueryRow(&list)
		if err == nil {
   logs.Info("this is notice")
		    // fmt.Println(lists[0][0]) // slene

		    // userId := fmt.Println()
		     c.Data["json"] = map[string]interface{}{
            "code":       200,
            "message":    "登录成功",
            "data":   list,
        }
        session.Set("loginuser", '123')
		}else{
                      
			c.Data["json"] = Respose{Code: 100,Msg: "登录失败!!!，没有输入密码",Pwd:pwd,UserId:0}
		}
 
        c.ServeJSON()


}


type RegisterController struct {
        beego.Controller
}


func (c *RegisterController) Get() {
        // var username string
        // var pwd string
        // var  user User
        // data := c.Ctx.Input.RequestBody
        // json.Unmarshal(data, &user)
        // username = user.UserName
        // pwd =  user.Pwd
        	username:=c.GetString("username")
	pwd:=c.GetString("pwd")
        if username == "" {
         c.Data["json"] = Respose{Code: 100,Msg: "注册失败，没有设置用户名",Pwd:pwd,UserId:0}
         c.ServeJSON()
         return
    }
    if pwd == "" {
         c.Data["json"] = Respose{Code: 100,Msg: "注册失败，没有设置密码",Pwd:pwd,UserId:0}
         c.ServeJSON()
         return
    }
        userId := rand.Intn(100)

        	//向数据库插入数据
	//1.有ORM对象
	o:=orm.NewOrm()
	//2.有一个要插入数据的结构体对象
	users:=models.Users{}
	//3.对结构体赋值
    	users.Username = username
       	users.Pwd = pwd
		users.AddTime = time.Now().Format("2006-01-02 15:04:05")
		users.UserId = userId

// 三个返回参数依次为：是否新创建的，对象 Id 值，错误
if created, id,err := o.ReadOrCreate(&users, "Username"); err == nil {
    if created {
		if err != nil {
	         c.Data["json"] = Respose{Code: 100,Msg: "注册失败，保存数据失败",Pwd:pwd,UserId:0}
	         c.ServeJSON()
			return
		}
    } else {
	    
	     c.Data["json"] = Respose{Code: 100,Msg: "用户名已存在",Pwd:pwd,UserId:int(id)}
         c.ServeJSON()
         return
    }
}



        c.Data["json"] = Respose{Code: 200,Msg: "注册成功",Pwd:pwd,UserId:userId}

        c.ServeJSON()

        
}